package cn.org.donald.framework.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @author ： Donald
 * @date ： 2020/10/17 17:21
 * @description： 获取bean工厂的bean实例
 */
@Component
public class ApplicationContextUtil implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        ApplicationContextUtil.applicationContext = applicationContext;
    }

    /**
     * 根据bean名称获取bean实例对象
     * @param beanName bean名称,spring默认以原名称首字母变小写命名
     * @return
     */
    public static Object getBean(String beanName){
        return applicationContext.getBean(beanName);
    }
}
